#include<bits/stdc++.h>
using namespace std;
namespace IO{
    const int BUF=1<<21;
    char buff[BUF],*p1=buff,*p2=buff;
    #define gc() (p1==p2&&(p2=((p1=buff)+fread(buff,1,BUF,stdin)),p1==p2)?EOF:*p1++)
    template<typename T>inline void read(T &x){
        char ch=gc();x=0;int f=1;
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
        x*=f;
    }
}
using namespace IO;
const int N=4e5+5;
int n,m,du[N],vis[N],ans,f[N],h[N],sz[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int main(){
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	read(m);n=m*2;
	for(int i=1;i<=n;++i) f[i]=i;
	while(m--){
		int x,y;
		read(x),read(y);
		x=find(x),y=find(y);
		if(x==y) h[x]=1;
		else f[y]=x,h[x]|=h[y];
	}
	for(int i=1;i<=n;++i) ++sz[find(i)];
	for(int i=1;i<=n;++i)if(f[i]==i) {
		if(h[i]) ans+=sz[i];
		else ans+=sz[i]-1;
	}
	cout<<ans;
	return 0;
}


